<?php

require_once 'database.php';
require_once 'configu.php';	
class user {
	static function isRoleAdmin($username){
        $result = database::query("SELECT * FROM user WHERE username='$username'");
        $usernya = mysql_fetch_array($result);
		return $usernya["Role"]==1;	
	}
	
	static function getName($username){
        $result = database::query("SELECT * FROM user WHERE username='$username'");
        $usernya = mysql_fetch_array($result);
		return $usernya["Nama"];	
	}
	static function getUser($username){
        $result = database::query("SELECT * FROM user WHERE username='$username'");
        $usernya = mysql_fetch_array($result);
		return $usernya;	
	}
	
	static function getAllPost($username){
        $result = database::query("SELECT IDPost FROM posttag WHERE username = '".$username."' ORDER BY IDPost DESC");
		return $result;
	}
	static function getAllCommentPost($idpost){
        $result = database::query("SELECT * FROM postkomen WHERE IDPost='$idpost'");
		return $result;
	}
	
	static function getAllNotif($username){
		return database::query("SELECT * FROM notification WHERE username='$username' ORDER BY idnotif DESC");
	}
	
	
	static function getNFollower($username){
		return mysql_num_rows(database::query("SELECT Follower FROM follownet WHERE Username = '$username'"));
	}
	static function getNFollowee($username){
		return mysql_num_rows(database::query("SELECT Username FROM follownet WHERE Follower = '$username'"));
	}

	/*
		Kembalian 0, semua tidak ada hubungan
		1 kalo uname1 punya folower 2
		2 kalo uname2 punya folower 1
		3 kalo saling memfolow
	*/
	static function checkFollow($uname1, $uname2){
		require_once 'database.php';
		$oneftwo = database::query("SELECT * FROM follownet WHERE username='$uname1' and follower='$uname2'");
		$roneftwo = mysql_fetch_array($oneftwo);
		
		$twofone = database::query("SELECT * FROM follownet WHERE username='$uname2' and follower='$uname1'");
		$rtwofone = mysql_fetch_array($twofone);
		
		if (isset($roneftwo["Username"])&&isset($rtwofone["Username"])){
			return 3;
		}
		else if (isset($roneftwo["Username"])&&!isset($rtwofone["Username"])){
			return 1;
		}
		else if (!isset($roneftwo["Username"])&&isset($rtwofone["Username"])){
			return 2;
		}
		else if (!isset($roneftwo["Username"])&&!isset($rtwofone["Username"])){
			return 0;
		}
	}
	
	static function checkHakAksesWall($me, $her){
		session_start();
		$bolehDiakses = false;
		$hubFollow = user::checkFollow($her,$me);
		$heruser = user::getUser($her);
		if ($me==$her||user::isRoleAdmin($me)){$bolehDiakses=true;}
		else if ($heruser["HakAksesWall"]==0){$bolehDiakses=false;	}
		else if ($heruser["HakAksesWall"]==1){
			if ($hubFollow==1||$hubFollow==3){$bolehDiakses=true;}
		}
		else if ($heruser["HakAksesWall"]==2&&$_SESSION['username']==$me){$bolehDiakses=true;}
		else if ($heruser["HakAksesWall"]==3){$bolehDiakses=true;}
		return $bolehDiakses;
	}
	
}
?>